const {
	Service
} = require('uni-cloud-router')
const config = require("../config-uni-id-member.js")
const uniID = require('uni-id')

const getUniID = (context) => {
	return uniID.createInstance({
		context,
		config
	})
}
module.exports = class WxPubUserService extends Service {
	async getNeareastCity({
		userLocation
	}) {
		const dbCmd = this.db.command
		try {
			if (userLocation) {
				let res = await this.db.collection("t3ds-region").where({
					location: dbCmd.geoNear({
						geometry: new this.db.Geo.Point(userLocation.longitude, userLocation
							.latitude)
					})
				}).field({
					city: true,
					name: true,
					wxPubConfig: true
				}).limit(1).get()
				if (res.data.length) {
					let data = res.data[0]
					delete data.wxPubConfig.appId
					delete data.wxPubConfig.appSecret
					return data
				}
			} else {
				let res = await this.db.collection("t3ds-region").field({
					city: true,
					name: true,
					wxPubConfig: true
				}).limit(1).get()
				if (res.data.length) {
					let data = res.data[0]
					delete data.wxPubConfig.appId
					delete data.wxPubConfig.appSecret
					return data
				}
			}

		} catch (e) {
			throw e
		}
	}
}
